﻿using HpxWMS_Api.BLL;
using HpxWMS_Api.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace HpxWMS_Api.Controllers
{
    /// <summary>
    /// 仓库管理系统
    /// </summary>
    public class WarehouseController : ApiController
    {
        private WarehouseManage warehousemanage = new WarehouseManage();
        /// <summary>
        /// 查询仓库
        /// </summary>
        /// <returns></returns>
        public R<WarehousePageVO> Get([FromUri] Wq wq)
        {
            //判断是否有传值，如果没有则返回默认值
            if (wq == null)
            {
                wq = new Wq();
            }
            //查询列表
            var list = warehousemanage.FindPage(wq);
            //查询总记录数
            var total = warehousemanage.Count(wq.SearchText);
            WarehousePageVO pageVO = new WarehousePageVO();
            pageVO.Total = total;
            pageVO.List = list;
            pageVO.PageIndex = wq.PageIndex;
            pageVO.pageSize = wq.PageSize;
            return new R<WarehousePageVO> { Code = "000", Message = "查询成功", Data = pageVO };
        }
        /// <summary>
        /// 添加仓库
        /// </summary>
        public R<WarehouseDTO> Post([FromBody] Wi wi)
        {
            if (!ModelState.IsValid)
            {
                throw new ClientException(HttpStatusCode.BadRequest);
            }
            if (warehousemanage.Insert(wi))
            {
                return new R<WarehouseDTO>() { Code = "000", Message = "添加成功" };
            }
            else
            {
                return new R<WarehouseDTO>() { Code = "999", Message = "添加失败" };
            }
        }
        /// <summary>
        /// 更新仓库
        /// </summary>
        public R<WarehouseDTO> Put([FromBody] Wu wu)
        {
            if (!ModelState.IsValid)
            {
                throw new ClientException(HttpStatusCode.BadRequest);
            }
            if (warehousemanage.Update(wu))
            {
                return new R<WarehouseDTO> { Code = "000", Message = "更新成功" };
            }
            else
            {
                return new R<WarehouseDTO> { Code = "999", Message = "更新失败" };
            }
        }

        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="id"></param>
        public R<WarehouseDTO> Delete(int id)
        {
            if (warehousemanage.Remove(id))
            {
                return new R<WarehouseDTO> { Code = "000", Message = "删除成功" };
            }
            else
            {
                return new R<WarehouseDTO> { Code = "999", Message = "删除失败" };
            }
        }
    }
}
